package com.android.controller_bt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.wangbl.manager.listener.NotifyListener;
import com.wangbl.manager.listener.SimpleNotifyListener;
import com.wangbl.manager.model.DevInfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	private static final int STATE_SCANNING = 1;
	private static final int STATE_FINISH = 2;
	private ListView listview;
	private Button btn;
	private List<DevInfo> devInfos = new ArrayList<DevInfo>();
	private BaseAdapter adapter;
	private int currState = STATE_FINISH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview = (ListView) findViewById(R.id.scan_listview);
		btn = (Button) findViewById(R.id.scan_btn);
		currState = STATE_SCANNING;
		adapter = new MyAdapter();
		
		listview.setAdapter(adapter);
		btn.setOnClickListener(this);
		listview.setOnItemClickListener(this);
		pd.setCancelable(false);
//		startControlActivity();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		manager.register(listener);
		manager.scanDevice();
		btn.setText("正在扫描...");
		btn.setEnabled(false);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		manager.unregister(listener);
		manager.stopScan();
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return devInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return devInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view ;
			Holder holder;
			DevInfo info = devInfos.get(position);
			if(convertView == null){
				view = View.inflate(MainActivity.this, R.layout.item_scan_listview, null);
				holder = new Holder();
				holder.tv_name = (TextView) view.findViewById(R.id.scan_dev_name);
				holder.tv_mac = (TextView) view.findViewById(R.id.scan_dev_mac);
				view.setTag(holder);
			} else {
				view = convertView;
				holder = (Holder) view.getTag();
			}
			if(TextUtils.isEmpty(info.getName())){
				holder.tv_name.setText("蓝牙名称：未知");
			} else {
				holder.tv_name.setText("蓝牙名称："+info.getName());
			}
			
			holder.tv_mac.setText("MAC 地址："+info.getMac());
			
			return view;
		}
		
	}
	
	private class Holder{
		public TextView tv_name;
		public TextView tv_mac;
	}
	
	private NotifyListener listener = new SimpleNotifyListener(){
		
		@Override
		public void onFindDevSuccess(HashMap<String,DevInfo> devInfos) {
			MainActivity.this.devInfos = new ArrayList<DevInfo>(devInfos.values());
			adapter.notifyDataSetChanged();
		};
		
		@Override
		public void onScanCloseBt() {
			currState = STATE_FINISH;
			btn.setText("重新扫描");
			btn.setEnabled(true);
		};
		
		@Override
		public void onScanFinish() {
			currState = STATE_FINISH;
			btn.setText("重新扫描");
			btn.setEnabled(true);
		};
		
		@Override
		public void onConnDevSuccess() {
			dismiss();
			toast("连接成功...");
			startControlActivity();
		};
		
		@Override
		public void onConnDevFail() {
			dismiss();
			toast("连接失败，请重新扫描...");
		};
	};
	
	
	private void startControlActivity(){
		Intent intent = new Intent(this, ControllerActivity.class);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.scan_btn:
			manager.scanDevice();
			btn.setText("正在扫描...");
			btn.setEnabled(false);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		manager.stopScan();
		show();
		DevInfo info = devInfos.get(position);
		manager.connDevice(info);
	}

}
